//
//  HTHomeController.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/16.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit

class HTHomeController: HTLoginBoolController {
    
    private lazy var stautsViewModel : HTStatsViewModel = HTStatsViewModel()
    
    //var statusArray : [HTStats]?
    
    let identer = "identer"
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !userLogin {
            
           loginBoolView.setControllerImage(nil, message: nil)
            
            return
        }
        
        setupUI()
        
        loadData()
        
    }

    
    private func setupUI(){
        
       // tableView.backgroundView = UIImageView(image: UIImage(named: "1"))
        
        tableView.registerClass(HTStatusCell.self, forCellReuseIdentifier: identer)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 250
        //隐藏分割线
        tableView.separatorStyle = .None
        tableView.tableFooterView = pullupView
        
        self.tableView.addSubview(htRefreshControl)
        
        htRefreshControl.addTarget(self, action: "loadData", forControlEvents:UIControlEvents .ValueChanged)
    }
    
    
    @objc private func loadData(){
        
        
        stautsViewModel.loadData (pullupView.isAnimating()){(isSuccess) -> () in
            
            if isSuccess {
            
                self.tableView.reloadData()
            }else{
            
                print("错误")
            }
            
            
        }
        
        self.endRefres()
        
    }
    
    
    func endRefres(){
    
        pullupView.stopAnimating()
        
        self.refreshControl?.endRefreshing()
        
        htRefreshControl.endRefeshing()
    }
    
    private lazy var pullupView : UIActivityIndicatorView = {
    
        let indeicator = UIActivityIndicatorView(activityIndicatorStyle:
            UIActivityIndicatorViewStyle.WhiteLarge)
        
        indeicator.color = UIColor.blueColor()
        
        indeicator.hidesWhenStopped = false
        
        return indeicator
    }()
    
    //添加下来控件
    private lazy var htRefreshControl : HTRefreshControl = HTRefreshControl()
    
    
    
    
    
    
    
    
    
    
    
//    private func loadData(){
//    
//        // 1. 定义请求地址
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        // 2. 定义请求参数
//        let params = [
//            "access_token": HTViewModel.sharedViewModel.accessToken ?? ""
//        ]
//        
//        
//        NetworkTools.sharedTools.request(.Get, urlString: urlString, parameters: params) { (responseObject, error) -> () in
//            
//            if error != nil {
//                
//                return
//            }
//            
//            guard let responseDict = responseObject as? [String : AnyObject] else {
//            
//                return
//            
//            }
//            
//            guard let dicts = responseDict["statuses"] as? [[String : AnyObject]] else {
//            
//                return
//            }
//            
//            
//            var tempArray = [HTStats]()
//            
//            for value in dicts{
//            
//                let status = HTStats(dict: value)
//                tempArray.append(status)
//            }
//            
//            print(tempArray)
//            self.statusArray = tempArray
//            self.tableView.reloadData()
//        }
//    }
//    
//    
}

extension HTHomeController {
    
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == stautsViewModel.status!.count - 1 && !pullupView.isAnimating(){
        
            pullupView.startAnimating()
            loadData()
        }
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return stautsViewModel.status?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(identer, forIndexPath: indexPath) as! HTStatusCell
        
//cell.textLabel?.text = stautsViewModel.status![indexPath.row].text
        
        cell.statusViewModel = stautsViewModel.status![indexPath.row]
        
        return cell
    }
}
